package ar.org.cfired.expedientes;



import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/* 
import java.sql.Connection;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext; 
import javax.sql.DataSource;
*/
public class HibernateUtil {
	
	private static Logger objetoLogger = Logger.getLogger("ar.org.cfired.expedientes.HibernateUtil");

    private static final SessionFactory sessionFactory = buildSessionFactory();
    private static final SessionFactory sessionFactoryJBPM = buildSessionFactoryJBPM();

    private static final String archivo = "ar/org/cfired/expedientes/expedientes.cfg.xml";
    private static final String archivoJBPM = "ar/org/cfired/expedientes/expedientes.cfg.xml";
    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
        	//JbpmDS
        	//CreditosDS
        	//InitialContext m = new InitialContext();
  //      	Context ctx = new InitialContext();
//        	return (SessionFactory) ctx.lookup("java:/hibernate/CreditosHibernateFactory"); 
        	//return new Configuration().configure().buildSessionFactory();
        	//return new Configuration.;
            return new Configuration().configure(archivo).buildSessionFactory();
            //aparentemente ve el archivo pero no puede parsearlo; revisar.
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            objetoLogger.error("Initial SessionFactory creation failed." + ex);

            throw new ExceptionInInitializerError(ex);
        }
    }
    
  public static SessionFactory buildSessionFactoryJBPM() {
        try {
  /*      	Properties properties = new Properties();
        	properties.setProperty(InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
        	properties.setProperty(InitialContext.PROVIDER_URL,"jnp://localhost:1099");

        	InitialContext ic = new InitialContext(properties);
        	if(ic!=null){
        		DataSource ds = (DataSource)ic.lookup("JbpmDS");
        		Connection con = ds.getConnection();
        	}
*/        	
        	//return new Configuration().configure("java:JbpmDS").buildSessionFactory();
            return new Configuration().configure(archivoJBPM).buildSessionFactory();
            //aparentemente ve el archivo pero no puede parsearlo; revisar.
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            objetoLogger.error("Initial SessionFactory creation failed." + ex);

            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
  
    public static SessionFactory getSessionFactoryJBPM() {
    	return sessionFactoryJBPM;
    }

    public String getArchivo() {
    	return(archivo);
    }
}
